<template>
  <section class="login-box">
    <div class="logo-img">
      <img src="../../common/image/nav1.png" alt="">
    </div>
    <div>
      <input v-model="loginForm.username" ref="loginInputUser" placeholder="请输入用户名" autocomplete="off"/>
      <input v-model="loginForm.password" ref="loginInputPwd" placeholder="请输入密码" autocomplete="off"
          @keydown.13="loginIn('loginForm')"/>
    </div>
      <p class="error" v-show="error.status"><i class="el-icon-warning"></i>{{error.text}}</p>
      <div class="form-item">
        <a class="btn-login" @click="loginIn('loginForm')">确定</a>
      </div>
      <a class="form-item btn-register" @click="goPage('Register')">
        还没有帐户？<span class="color-blue">立即注册</span>
      </a>
  </section>
</template>

<script>
import { Toast } from 'vant'
import { getCookie } from '../../untils/storage.js'
export default {
  components: {
    [Toast.name]: Toast
  },
  data () {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      error: {}
    }
  },
  methods: {
    goPage (toutername) {
      this.$router.push({ name: toutername })
    },
    loginIn (formname) {
      if (!this.loginForm.username) {
        this.$message.error('请输入用户名')
        return false
      } else if (!this.loginForm.password) {
        this.$message.error('请输入密码')
        return false
      }
      this.$store.dispatch('GETUSER', this.loginForm).then((res, rej) => {
        // return 的数据
        if (res.resultCode === 2000) {
          const needStatus = getCookie('needUpdateStatus')
          const Exam = parseInt(getCookie('Exam'))
          // console.log('登陆')
          // debugger
          if (needStatus === 'true' || Exam === 3) {
            this.$store.commit('EXAMNODO', true)
          }
          this.$router.push(this.$route.query.redirect || '/')
        } else {
          this.error.status = true
          this.error.text = res.resultMsg
          this.loginForm.password = ''
          return false
        }
      })
    }
  }
}
</script>

<style lang="stylus" scoped>

</style>
